Micron Document
🎖️GitЯра🎖️


Displaying Raw • Download

core/ble/src/commonMain/kotlin/org/meshtastic/core/ble/BleRetry.kt 2d20cd8a4708e2ef66e98d5259f3a97ec93f240a (2d20cd8a) Text, 3.46 KB

T8b949e/*
* Copyright (c) 2026 Meshtastic LLC
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
Tff7b72package T7ee787org.meshtastic.core.ble

Tff7b72import T7ee787co.touchlab.kermit.Logger
Tff7b72import T7ee787kotlinx.coroutines.CancellationException
Tff7b72import T7ee787kotlinx.coroutines.delay
Tff7b72import T7ee787kotlin.math.pow
Tff7b72import T7ee787kotlin.random.Random

T8b949e/** Cap on the per-attempt backoff to prevent unbounded growth. */
Tff7b72private Tff7b72const Tff7b72val Te6edf3MAX_RETRY_DELAY_MS Tff7b72= T79c0ff2Te6edf3_000L

T8b949e/** Multiplicative growth factor between attempts (delay doubles each time). */
Tff7b72private Tff7b72const Tff7b72val Te6edf3BACKOFF_FACTOR Tff7b72= T79c0ff2.0

T8b949e/**
* Retries a BLE operation with bounded exponential backoff and jitter.
*
* Each retry waits `delayMs * 2^(attempt-1)`, capped at [MAX_RETRY_DELAY_MS], with a random ±25% jitter applied to
* avoid synchronised retry storms when multiple operations fail in lockstep (e.g. a TX/RX pair both failing the same
* `STATUS_GATT_BUSY` window).
*
* @param count Total attempt count (default 3).
* @param delayMs Initial delay before the first retry. Subsequent delays grow exponentially.
* @param tag Tag for log prefixes.
* @param retryWhile Checked after a failed attempt and again before each retry. Returning false rethrows the latest
* failure immediately without logging or waiting, which lets callers revoke retries when the operation's session is
* no longer current. The initial attempt always runs.
* @param block The operation to perform.
* @return The result of the operation.
* @throws Exception If the operation fails after all attempts. [CancellationException] is always re-thrown immediately.
*/
Tf0883e@SuppressTb4b4b4(Ta5d6ff"Ta5d6ffMagicNumberTa5d6ff"Tb4b4b4, Ta5d6ff"Ta5d6ffThrowsCountTa5d6ff"Tb4b4b4)
Tff7b72suspend Tff7b72fun Tff7b72<Te6edf3TTff7b72> Td2a8ffretryBleOperationTb4b4b4(
Te6edf3countTb4b4b4: Tffa657Int Tff7b72= T79c0ff3Tb4b4b4,
Te6edf3delayMsTb4b4b4: Tffa657Long Tff7b72= T79c0ff2T79c0ff5T79c0ff0LTb4b4b4,
Te6edf3tagTb4b4b4: Tffa657String Tff7b72= Ta5d6ff"Ta5d6ffBLETa5d6ff"Tb4b4b4,
Te6edf3retryWhileTb4b4b4: Tb4b4b4(Tb4b4b4) Tff7b72-Tff7b72> Tffa657Boolean Tff7b72= Tb4b4b4{ Tff7b72true Tb4b4b4}Tb4b4b4,
Te6edf3blockTb4b4b4: Te6edf3suspend Tb4b4b4(Tb4b4b4) Tff7b72-Tff7b72> Te6edf3TTb4b4b4,
Tb4b4b4)Tb4b4b4: Te6edf3T Tb4b4b4{
Tff7b72var Te6edf3currentAttempt Tff7b72= T79c0ff0
Tff7b72var Te6edf3lastFailureTb4b4b4: Te6edf3Exception? Tff7b72= Tff7b72null
Tff7b72while Tb4b4b4(Tff7b72trueTb4b4b4) Tb4b4b4{
Te6edf3lastFailureTff7b72?.Te6edf3let Tb4b4b4{ Tff7b72if Tb4b4b4(Tff7b72!Te6edf3retryWhileTb4b4b4(Tb4b4b4)Tb4b4b4) Tff7b72throw Tffa657it Tb4b4b4}
Tff7b72try Tb4b4b4{
Tff7b72return Te6edf3blockTb4b4b4(Tb4b4b4)
Tb4b4b4} Tff7b72catch Tb4b4b4(Te6edf3eTb4b4b4: Te6edf3CancellationExceptionTb4b4b4) Tb4b4b4{
Tff7b72throw Te6edf3e
Tb4b4b4} Tff7b72catch Tb4b4b4(Tf0883e@SuppressTb4b4b4(Ta5d6ff"Ta5d6ffTooGenericExceptionCaughtTa5d6ff"Tb4b4b4) Te6edf3eTb4b4b4: Te6edf3ExceptionTb4b4b4) Tb4b4b4{
Te6edf3lastFailure Tff7b72= Te6edf3e
Tff7b72if Tb4b4b4(Tff7b72!Te6edf3retryWhileTb4b4b4(Tb4b4b4)Tb4b4b4) Tff7b72throw Te6edf3e
Te6edf3currentAttemptTff7b72+Tff7b72+
Tff7b72if Tb4b4b4(Te6edf3currentAttempt Tff7b72>Tff7b72= Te6edf3countTb4b4b4) Tb4b4b4{
Te6edf3LoggerTb4b4b4.Te6edf3wTb4b4b4(Te6edf3eTb4b4b4) Tb4b4b4{ Ta5d6ff"Ta5d6ff[Tffd700$Te6edf3tagTa5d6ff] BLE operation failed after Tffd700$Te6edf3countTa5d6ff attempts, giving upTa5d6ff" Tb4b4b4}
Tff7b72throw Te6edf3e
Tb4b4b4}
Tff7b72val Te6edf3backoffMs Tff7b72= Tb4b4b4(Te6edf3delayMs Tff7b72* Te6edf3BACKOFF_FACTORTb4b4b4.Te6edf3powTb4b4b4(Te6edf3currentAttempt Tff7b72- T79c0ff1Tb4b4b4)Tb4b4b4)Tb4b4b4.Te6edf3toLongTb4b4b4(Tb4b4b4)Tb4b4b4.Te6edf3coerceAtMostTb4b4b4(Te6edf3MAX_RETRY_DELAY_MSTb4b4b4)
Tff7b72val Te6edf3jitterRange Tff7b72= Tb4b4b4(Te6edf3backoffMs Tff7b72/ T79c0ff4Tb4b4b4)Tb4b4b4.Te6edf3coerceAtLeastTb4b4b4(T79c0ff1LTb4b4b4)
Tff7b72val Te6edf3jitter Tff7b72= Te6edf3RandomTb4b4b4.Te6edf3nextLongTb4b4b4(Tff7b72-Te6edf3jitterRangeTb4b4b4, Te6edf3jitterRange Tff7b72+ T79c0ff1Tb4b4b4)
Tff7b72val Te6edf3sleepMs Tff7b72= Tb4b4b4(Te6edf3backoffMs Tff7b72+ Te6edf3jitterTb4b4b4)Tb4b4b4.Te6edf3coerceAtLeastTb4b4b4(T79c0ff0LTb4b4b4)
Te6edf3LoggerTb4b4b4.Te6edf3wTb4b4b4(Te6edf3eTb4b4b4) Tb4b4b4{ Ta5d6ff"Ta5d6ff[Tffd700$Te6edf3tagTa5d6ff] BLE operation failed (attempt Tffd700$Te6edf3currentAttemptTa5d6ff/Tffd700$Te6edf3countTa5d6ff), retrying in Tffd700${Te6edf3sleepMsTffd700}Ta5d6ffms...Ta5d6ff" Tb4b4b4}
Te6edf3delayTb4b4b4(Te6edf3sleepMsTb4b4b4)
Tb4b4b4}
Tb4b4b4}
Tb4b4b4}

Served by rngit 1.5.0 - Generated in 0.06s